package com.hs.shirodemo2.controller;

import com.hs.shirodemo2.constant.CommonConstant;
import com.hs.shirodemo2.entity.SysPermission;
import com.hs.shirodemo2.entity.UserInfo;
import com.hs.shirodemo2.service.MenuService;
import com.hs.shirodemo2.service.SysPermissionService;
import com.hs.shirodemo2.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/permission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private MenuService menuService;

    /**
     * 新增
     */
    @RequiresPermissions("permission:add")
    @PostMapping("/add")
    public Result add(@RequestBody SysPermission sysPermission) {
        sysPermissionService.add(sysPermission);
        return Result.success();
    }

    /**
     * 编辑
     */
    @RequiresPermissions("permission:edit")
    @PutMapping("/edit")
    public Result edit(@RequestBody SysPermission sysPermission) {
        sysPermissionService.edit(sysPermission);
        return Result.success();
    }

    /**
     * 删除
     */
    @RequiresPermissions("permission:remove")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        sysPermissionService.remove(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @RequiresPermissions("permission:remove")
    @DeleteMapping("/remove")
    public Result removeBatch(Integer[] ids) {
        sysPermissionService.removeBatch(ids);
        return Result.success();
    }

    /**
     * 单条查询
     */
    @RequiresPermissions("permission:query")
    @GetMapping("/find/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(sysPermissionService.findById(id));
    }

    /**
     * 分页查询
     */
    @RequiresPermissions("permission:query")
    @PostMapping("/findPage/{pageNum}/{pageSize}")
    public Result findPage( @PathVariable Integer pageNum,
                            @PathVariable Integer pageSize,
                            @RequestBody SysPermission sysPermission) {
        return Result.success(sysPermissionService.findPage(pageNum, pageSize, sysPermission));
    }

    /**
     * 查询
     */
    @RequiresPermissions("permission:query")
    @GetMapping("/find")
    public Result find(SysPermission sysPermission) {
        return Result.success(sysPermissionService.find(sysPermission));
    }

    /*
    * 查询权限目录和权限列表
    * */
    @GetMapping("/findMenuAndList")
    public Result findMenuAndList(){
        return Result.success(sysPermissionService.selectPermissionMenuAndList());
    }

    /*
    * 查询当前用户的权限菜单
    * */
    @GetMapping("/findMenu")
    public Result findMenu(HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        List<SysPermission> menu = sysPermissionService.findMenu(userInfo.getId());
        return Result.success(menu);
    }

    @GetMapping("/findMenuByUserId/{id}")
    public Result findMenuByUserId(@PathVariable Integer id){
        List<SysPermission> permissionByUserId = sysPermissionService.findPermissionByUserId(id);
        List<SysPermission> menuTree = menuService.buildMenuTree(permissionByUserId);
        return Result.success(menuTree);
    }

    @GetMapping("/findButtonByUserId/{id}")
    public Result findButtonByUserId(@PathVariable Integer id){
        List<SysPermission> permissionByUserId = sysPermissionService.findPermissionByUserId(id);
        List<String> buttonList = permissionByUserId.stream()
                .filter(sysPermission -> "3".equals(sysPermission.getType()))
                .map(SysPermission::getPercode).collect(Collectors.toList());
        return Result.success(buttonList);
    }
}